package cn.iocoder.yudao.module.member.service.major;

import cn.iocoder.yudao.module.member.controller.admin.majorobtain.vo.MajorObtainRespVO;
import cn.iocoder.yudao.module.member.dal.dataobject.majorobtain.MajorObtainDO;
import cn.iocoder.yudao.module.member.dal.mysql.majorobtain.MajorObtainMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.member.controller.admin.major.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.major.MajorDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.major.MajorMapper;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 专业 Service 实现类
 *
 * @author Alan
 */
@Service
@Validated
public class MajorServiceImpl implements MajorService {
    @Resource
    private MajorObtainMapper majorObtainMapper;
    @Resource
    private MajorMapper majorMapper;

    @Override
    public Long createMajor(MajorSaveReqVO createReqVO) {
        // 插入
        MajorDO major = BeanUtils.toBean(createReqVO, MajorDO.class);
        majorMapper.insert(major);
        // 返回
        return major.getId();
    }

    @Override
    public void updateMajor(MajorSaveReqVO updateReqVO) {
        // 校验存在
        validateMajorExists(updateReqVO.getId());
        // 更新
        MajorDO updateObj = BeanUtils.toBean(updateReqVO, MajorDO.class);
        majorMapper.updateById(updateObj);
    }

    @Override
    public void deleteMajor(Long id) {
        // 校验存在
        validateMajorExists(id);
        // 删除
        majorMapper.deleteById(id);
    }

    private void validateMajorExists(Long id) {
        if (majorMapper.selectById(id) == null) {
            throw exception(MAJOR_NOT_EXISTS);
        }
    }

    @Override
    public MajorDO getMajor(Long id) {
        MajorDO majorDO = majorMapper.selectById(id);
        List<MajorObtainDO> majorObtainDOS = majorObtainMapper.selectList(new LambdaQueryWrapper<MajorObtainDO>()
                .eq(MajorObtainDO::getMajorId, id));
        if (Objects.nonNull(majorDO)){
            majorDO.setMajorObtain(majorObtainDOS);
        }
        return majorDO;
    }

    @Override
    public PageResult<MajorDO> getMajorPage(MajorPageReqVO pageReqVO) {
        return majorMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MajorDO> getMajorOneList() {
        return majorMapper.selectList(new LambdaQueryWrapper<MajorDO>().eq(MajorDO::getHasChild,"1"));
    }

    @Override
    public List<MajorDO> getMajorTwoList(Long pid) {
        return majorMapper.selectList(new LambdaQueryWrapper<MajorDO>().eq(MajorDO::getHasChild,"2")
                .eq(MajorDO::getPid,pid));
    }

    @Override
    public List<MajorDO> getMajorThreeList(Long pid) {
        return majorMapper.selectList(new LambdaQueryWrapper<MajorDO>().eq(MajorDO::getHasChild,"3").eq(MajorDO::getPid,pid));
    }

    @Override
    public List<MajorDO> getSimpleList() {
        return majorMapper.selectList();
    }

    @Override
    public List<MajorRespVO> getMajorList(String name) {
        List<MajorRespVO> respVOS = new ArrayList<>();
        respVOS = majorMapper.getMajorList(name);
        return respVOS;
    }

    @Override
    public void importExcel(List<MajorRespVO> list) {
        List<MajorDO> majorObtainDOS =new ArrayList<>();
        List<MajorDO> majorDOS = majorMapper.selectList();
        for (MajorRespVO respVO:list) {
            MajorDO obtainDO =new MajorDO();
            BeanUtils.copyProperties(respVO,obtainDO);
            MajorDO majorDO = majorDOS.stream().filter(v1 -> v1.getName().equals(respVO.getParentName())).findFirst().orElse(null);
            if (Objects.nonNull(majorDO)) {
                obtainDO.setPid(majorDO.getId());
            }
            obtainDO.setHasChild("3");
            majorObtainDOS.add(obtainDO);
        }
        majorMapper.insertBatch(majorObtainDOS);
    }
}